<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper" style="position: relative">
        <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="onQuery">
          <div class="mode_item">
            <a-form-model-item label="手机号码">
              <a-input v-model="form.phone" placeholder="请输入" />
            </a-form-model-item>
            <a-form-model-item label="姓名/代号">
              <a-input v-model="form.name" placeholder="请输入" />
            </a-form-model-item>
            <a-form-model-item label="标签">
              <j-dict-select-tag v-model="form.labelName" placeholder="请选择" dictCode="sample_person_label" />
            </a-form-model-item>
            <a-form-model-item label="号码归属地">
              <a-input v-model="form.phoneArea" placeholder="请输入" />
            </a-form-model-item>
          </div>
          <div class="mode_item">
            <a-form-model-item label="关联案事件">
              <j-search-select-tag
                placeholder="请选择"
                v-model="form.incidentCode"
                :dictOptions="incidentCodeOptions"
              />
            </a-form-model-item>
            <a-form-model-item label="状态">
              <j-search-select-tag placeholder="请选择" v-model="form.status" :dictOptions="statusOptions" />
            </a-form-model-item>
            <a-form-model-item label="入库时间">
              <a-range-picker
                :allowClear="true"
                v-model="rangePickerTime"
                valueFormat="YYYY-MM-DD"
                format="YYYY-MM-DD"
                @change="rangePickerChange"
                style="width: 100%;"
              />
            </a-form-model-item>
          </div>
          <div style="margin-top: 20px">
            <a-form-model-item :wrapper-col="{ offset: 0 }" style="margin-bottom: 0">
              <a-button class="Indormation-list-CX" type="primary" @click="onQuery"> 查询 </a-button>
              <a-button class="Indormation-list-CX" style="margin-left: 10px" @click="onResetForm"> 重置 </a-button>
            </a-form-model-item>
          </div>
        </a-form-model>
      </div>
    </a-card>
    <a-card :bordered="false">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <div>
          <a-button @click="handleExport">导出</a-button>
          <a-button :disabled="isDisabled" @click="handleUpdateStatus(1)">移除</a-button>
          <a-button :disabled="isDisabled" @click="handleUpdateStatus(0)">恢复</a-button>
        </div>
      </div>
      <!-- 表格展示 -->
      <div class="table-page-content">
        <z-table
          ref="table"
          rowKey="id"
          :showAlert="false"
          :showSelection="true"
          :loading="loading"
          :columns="columns"
          :dataSource="pageData"
          @selectedRow="onSelectedRow"
          @change="onTableChange"
        >
          <template slot="status" slot-scope="text, record">
            {{ getLabel(statusOptions, text) }}
          </template>
          <!--操作-->
          <template slot="action" slot-scope="text, record">
            <a-button @click="handleDetails(record)" type="link" style="padding:0;">查看</a-button>
            <a-divider type="vertical" />
            <a-button @click="handleEdit(record)" type="link" style="padding:0;">编辑</a-button>
            <a-divider type="vertical" />
            <a-button type="link" style="padding:0;" v-if="record.status == 0" @click="handleUpdateStatus(1, record)"
              >移除</a-button
            >
            <a-button type="link" style="padding:0;" v-if="record.status == 1" @click="handleUpdateStatus(0, record)"
              >恢复</a-button
            >
          </template>
        </z-table>
      </div>
      <!--弹窗区域 -->
      <!-- pageType，1：案事件管理 2：任务情报列表 3： 配置情报列表 4： 重点人列表 5：任务情报管理 6：种子号码列表 7：协查函列表、8 审批列表   10 负样本人员库 11 时空碰撞 -->
      <handle-modal ref="handleModalRef" pageType="10" :title="handleModalTitle" />
      <edit-modal ref="editModalRef" title="编辑负样本人" type="samplesPersonnel" @ok="modalOk" />
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import * as peopleApi from '@/api/wxSmuggle/people.js'
import { getDictItemsFromCache } from '@/api/api'
import { downLoadFileReader } from '@/utils/export-util/util'
import handleModal from '../modules/CaseDetailModal'
import editModal from './modules/editModal'

export default {
  components: {
    handleModal,
    editModal
  },
  // mixins: [DisabledAuthFilterMixin],
  data() {
    return {
      moment,
      labelCol: { span: 6 },
      wrapperCol: { span: 17 },
      rangePickerTime: [],
      show: false,
      closeTip: false,
      loading: false,
      visible: false,
      isDisabled: true,
      reloading: false,
      selectKeys: [],
      form: {},
      handleModalTitle: null,
      incidentCodeOptions: [],

      statusOptions: [
        // { text: '全部', value: '', },
        { text: '正常', value: '0' },
        { text: '已移除', value: '1' }
      ],
      columns: [
        {
          title: '序号',
          width: 60,
          align: 'center',
          dataIndex: 'order',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '手机号码',
          dataIndex: 'phone',
          width: 115,
          align: 'center',
          scopedSlots: { customRender: 'phone' }
        },
        {
          title: '姓名/代号',
          dataIndex: 'name',
          width: 110,
          align: 'center',
          slots: { title: 'name' }
        },
        {
          title: '标签',
          dataIndex: 'labelName',
          width: 60,
          align: 'center',
          customRender: text => {
            return <j-ellipsis value={this.filterMultiDictText('sample_person_label', text)} length={8} />
          }
        },
        {
          title: '任务编号',
          dataIndex: 'taskCode',
          width: 100,
          align: 'center'
        },
        {
          title: '任务名称',
          dataIndex: 'taskName',
          width: 80,
          align: 'center'
        },
        {
          title: '案事件编号',
          dataIndex: 'incidentCode',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'incidentCode' }
        },
        {
          title: '案事件名称',
          dataIndex: 'incidentName',
          width: 100,
          align: 'center'
        },
        {
          title: '归属地',
          dataIndex: 'phoneArea',
          width: 100,
          align: 'center'
        },
        {
          title: '入库时间',
          dataIndex: 'createTime',
          width: 80,
          align: 'center'
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          width: 120,
          align: 'center',
          dataIndex: 'action',
          // slots: { title: 'customIsAction' },
          scopedSlots: { customRender: 'action' }
        }
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10
      },
      pageData: {}
    }
  },
  created() {},
  async mounted() {
    this.initList()
    this.onQuery()
  },
  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.form = {}
      this.rangePickerTime = []
      this.pageFilter.pageNo = 1
      this.pageFilter.pageSize = 10
      this.refreshTable()
    },
    onSelectedRow(keys) {
      this.selectKeys = keys
      this.isDisabled = keys.length > 0 ? false : true
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      let form = Object.assign({}, this.form, this.pageFilter)
      this.loading = true
      peopleApi
        .wxSamplePersonList(form)
        .then(res => {
          if (res.success) {
            this.pageData = res.result ? res.result : {}
            this.loading = false
            this.$refs.table.onClearRowKeys()
          } else {
            this.loading = false
          }
        })
        .catch(e => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    initList() {
      let data = [{ text: '全部', value: '' }]
      // 关联案事件
      peopleApi.getIncidentCodeList2().then(res => {
        if (res.success) {
          let datas =
            res.result && res.result.length > 0
              ? res.result.map(key => {
                  return { text: key, value: key }
                })
              : []
          this.incidentCodeOptions = [...data, ...datas]
        }
      })
    },
    // 导出
    handleExport(record) {
      let that = this
      let selectList = this.$refs.table.getSelectedRows()
      let idList1 = selectList.map(v => v.id).filter(obj => obj !== null && obj !== undefined && obj !== '')
      let idList2 = record && Object.keys(record).length > 0 && record.id ? [record.id] : []
      let params = Object.assign(
        {},
        {
          idList: selectList.length > 0 ? idList1 : idList2,
          samplePersonQuery: this.form
        }
      )
      this.$confirm({
        title: `是否确认导出？`,
        okText: '确定',
        onOk() {
          peopleApi.samplePersonExportXls(params).then(async res => {
            const fileName = '负样本人员库数据.xls'
            await downLoadFileReader(res, fileName)
          })
        }
      })
    },
    // 移除、恢复
    handleUpdateStatus(status, record) {
      let that = this
      let selectList = this.$refs.table.getSelectedRows()
      let idList1 = selectList.map(v => v.id).filter(obj => obj !== null && obj !== undefined && obj !== '')
      let idList2 = record && Object.keys(record).length > 0 && record.id ? [record.id] : []
      let params = Object.assign(
        {},
        {
          idList: selectList.length > 0 ? idList1 : idList2,
          operationType: status
        }
      )
      let title = status === 0 ? '恢复' : '移除'
      this.$confirm({
        title: `确定${title}选择的负样本人员？`,
        okText: '确定',
        onOk() {
          peopleApi
            .wxSamplePersonStatus(params)
            .then(async res => {
              if (res.success) {
                that.$message.success(res.message)
                that.onQuery()
              } else {
                that.$message.warn(res.message)
              }
            })
            .catch(e => {
              that.$message.warn('请求失败！')
            })
        }
      })
    },
    // 弹窗成功接收
    modalOk(type) {
      // 成功时，重载列表
      this.onQuery()
    },
    // 详情
    handleDetails(record) {
      this.handleModalTitle = '任务详情'
      this.$refs.handleModalRef.show(record)
    },
    handleEdit(record) {
      this.$refs.editModalRef.edit(record)
    },
    // 入库时间
    rangePickerChange(date) {
      this.$set(this.form, 'intoDatabaseStartTime', date[0])
      this.$set(this.form, 'intoDatabaseEndTime', date[1])
    },
    getLabel(datas, value) {
      let text = ''
      let item = datas.find(obj => {
        return obj.value == value
      })
      if (item && Object.keys(item).length > 0) {
        text = item.text
      }
      return text
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = true
      this.$nextTick(() => (this.reloading = false))
    },
    filterDictText(dictCode, text) {
      return this.$filterDictTextByCache(dictCode, text)
    },
    filterMultiDictText(dictCode, text) {
      if (!text && text !== 0) return
      let datas = getDictItemsFromCache(dictCode)
      return this.$filterDictText(datas, text)
    }
  }
}
</script>
<style lang="less" scoped>
.mode_item {
  display: flex;
  border-bottom: 1px #dadada dashed;
  > div {
    flex: 0 0 25%;
  }
  & > * {
    width: 100%;
    margin-bottom: 0;
    padding: 5px 0;
  }
  &.mode_item_none {
    > div {
      flex: inherit;
    }
  }
  .zdy-form-item-label {
    /deep/.ant-form-item-label {
      width: 70px;
    }
    &.xzqh-form-item-label {
      /deep/.ant-form-item-control-wrapper {
        .ant-form-item-children {
          display: -webkit-box;
        }
      }
      /deep/.area-select {
        // margin-top: 4px;
        line-height: 1;
      }
      /deep/.select-tag {
        width: 162px !important;
        margin-left: 10px;
        margin-top: 3px;
      }
      // /deep/.area-select .area-selected-trigger {
      //   line-height: 1.15;
      // }
      // /deep/.ant-select-lg .ant-select-selection--single{
      //   height: 34px;
      // }
      // /deep/.ant-select-lg .ant-select-selection__rendered {
      //   line-height: 34px;
      //   font-size: 14px;
      // }
    }
  }
}
.search-wrapper-btn {
  position: absolute;
  right: 0;
  top: 15px;
  z-index: 1;
}
/deep/.table-operator {
  display: flex;
  justify-content: space-between;
}
/deep/.table-custom-cols {
  top: -48px !important;
}
.custom-ewm {
  padding: 14px;
  background-color: #fff;
  border: 1px #ccc solid;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
}
/deep/.ant-dropdown-menu-item {
  text-align: center;
  border-bottom: 1px #f5f5f5 solid;
  &:last-child {
    border-bottom: 0;
  }
}
/deep/.anticon.anticon-down {
  font-size: 14px !important;
}

.table-page-content {
  /deep/.ant-table-thead > tr > th,
  /deep/.ant-table-tbody > tr > td {
    padding-left: 0;
    padding-right: 0;
  }
  /deep/.ant-table-thead > tr:first-child > th:first-child,
  /deep/.ant-table-tbody > tr > td:first-child {
    padding-left: 10px;
  }
}
</style>
